#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <X11/Xlib.h>
#include <tinywidgets/tnWidgets.h>
#include <X11/keysymdef.h>
#include "util.h"
#include "intfMsg.h"

const char * ir_name = "/dev/ir";
#if 1
const static int keymap [] = {
    0x42, XK_1,
    0x43, XK_2,
    0x0f, XK_3,
    0x1e, XK_4,
    0x1d, XK_5,
    0x1c, XK_6,
    0x18, XK_7,
    0x15, XK_8,
    0x16, XK_9,
    0x17, XK_0,

    0x57, XK_Prior,
    0x4d, XK_Next,
    0x55, XK_Pause,
    0x1a, XK_End,
    0x4f, XK_AudioRaiseVolume,
    0x48, XK_AudioLowerVolume,

    -1, -1
};

#else
const static int keymap [] = {
    0x04, XK_Execute,
    0x47, XK_Break,
    0x01, XK_Page_Up,
    0x07, XK_Page_Down,
//    0x02, XK_AudioLowerVolume,
    0x02, XK_KP_Subtract,
    0x03, XK_Menu,
//    0x41, XK_AudioRaiseVolume,
    0x41, XK_KP_Add,
    0x40, XK_space,
    0x0a, XK_1,
    0x0b, XK_2,
    0x49, XK_3,
    0x48, XK_4,
    0x0e, XK_5,
    0x0f, XK_6,
    0x4d, XK_7,
    0x4c, XK_8,
    0x12, XK_9,
    0x13, XK_0,
    0x51, XK_period,
    0x50, 0xff7e,
    0x17, XK_Up,
    0x1a, XK_Left,
    0x1b, XK_Return,
    0x59, XK_Right,
    0x58, XK_BackSpace,
    0x1f, XK_Down,
    0x5d, XK_Home,
    0x5c, XK_End,
    0x10, XK_Escape,
    0x15, XK_Tab,
    0x5a, XK_Redo,
    0x5f, XK_Undo,
    -1, -1
};
#endif

static int msqid_dest     = -1;

static void send_keypress(unsigned char key, int dummy)
{
    unsigned int xkey = 0;
    for (int i=0; keymap[i] != -1; i+=2) {
        if (key == keymap[i]) {
            xkey = keymap[i + 1];
            break;
        }
    }
    
    if (xkey != 0) {
        printf("key: %x, xkey: %x\n", key, xkey);
        RtnMsg msg;
        msg.msg_code = MSG_MAIN_IR_KEYCODE;
        msg.msg_len = sizeof(xkey);
        memcpy(msg.msg_data, &xkey, sizeof(xkey));
        SendMessage(msqid_dest, &msg);
    }
    else {
        printf("not mapped key: 0x%x\n", key);
    }
}

#if MYPLAYER
int ir_main(int msqid)
{
    msqid_dest     = msqid;
#else
int main(int argc, char ** argv)
{
    msqid_dest     = OpenMsgQueue(KEY_FOR_MAIN_MESSAGE);
#endif
    int fd = open(ir_name, O_RDONLY);
    if (!fd) {
        fprintf(stderr, "open %s failed.\n", ir_name);
        exit(-1);
    }
    unsigned char buf[4];
    int l;
    fd_set readfds;

    while (1) {
        FD_ZERO(&readfds);
        FD_SET(fd, &readfds);

        if (select(fd+1, &readfds, NULL, NULL, NULL) > 0) {
            if ((l = read(fd, buf, sizeof(buf))) == sizeof(buf)) {
                if (buf[0] == 0 &&  buf[1] == 0xff && (buf[2] + buf[3] == 0xff)) {
                    // printf("key: %02x %02x\n", buf[2], buf[3]);
                    send_keypress(buf[2], 0);
                }
                else {
                    fprintf(stderr, "ir code error { ");
                    for (int i=0; i<l; i++) {
                        fprintf(stderr, "%02x ", buf[i]);
                    }
                    fprintf(stderr, " }\n");
                }
            }
            else {
                fprintf(stderr, "read error: %s, len:%d\n", strerror(errno), l);
            }
        }
        else {
            fprintf(stderr, "select error:%s\n", strerror(errno));
        }
    }
    return 0;
}

